'''
    @author : walker
    @time : 2019/10/2
    @description : 对北京房价数据进行画图操作
'''

import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd

#设置画图的字体
plt.style.use("fivethirtyeight")
sns.set_style({'font.sans-serif':['simhei','Arial']})

class analysis_house_data(object):
    def __init__(self,house_data):
        self.house_data = house_data

    def region_analysis(self):
        '''
            对region进行特征分析
            param:
                house_data : 房屋数据
            return:
                pic : 绘制不同区域房价信息
        '''
        #对二手房区域进行分组对比
        #获取到每个区域中的二手房数量,之所以要在Region后面加上Price主要是以price这一列来统计数量
        region_house_count = self.house_data.groupby('Region')['Price'].count().sort_values(ascending = False).to_frame().reset_index()
        # print(region_house_count)
        #获取到每个区域的二手房均价
        region_house_mean = self.house_data.groupby('Region')['PerPrice'].mean().sort_values(ascending = False).to_frame().reset_index()
        # print(region_house_mean)

        f,ax1 = plt.subplots(1,1,figsize = (10,5))
        sns.barplot(x = 'Region',y = 'Price',palette = "Greens_d",data = region_house_count,ax = ax1)
        ax1.set_title('北京各大区二手房数量对比')
        # ax1.set_xlabel('区域')
        ax1.set_ylabel('数量')
        plt.show()

        f,ax2 = plt.subplots(1,1,figsize = (10,5))
        sns.barplot(x = 'Region',y = 'PerPrice',palette = "Blues_d",data = region_house_mean,ax = ax2)
        ax2.set_title('北京各大区二手房单价对比')
        # ax2.set_xlabel('区域')
        ax2.set_ylabel('每平米单价')
        plt.show()

        #
        # #查看每个区域价格的箱型图
        f,ax3 = plt.subplots(1,1,figsize = (10,5))
        sns.boxplot(x = 'Region',y = 'Price',data = self.house_data,ax = ax3)
        ax3.set_title('北京各大区二手房价格分布情况')
        # ax3.set_xlabel('区域')
        ax3.set_ylabel('价格分布情况')

        plt.show()

    def size_analysis(self):
        '''
            房屋大小size特征分析
            param:
                house_data : 房屋数据
            return:
                pit : 绘制不同区域房价信息
        '''
        f,[ax1,ax2] = plt.subplots(1,2,figsize = (15,7))

        #建立时间的分布情况
        sns.distplot(self.house_data['Size'],bins = 20,ax = ax1,color = 'r')
        sns.kdeplot(self.house_data['Size'],shade = True,ax = ax1)

        #建房时间和price的关系
        sns.regplot(x = 'Size',y = 'Price',data = self.house_data,ax = ax2)
        # print(house_data.loc[house_data['Size'] < 10])
        # print(house_data.loc[house_data['Size'] > 1000])
        plt.show()

    def layout_analysis(self):
        '''
            对房子的layout进行分，就是对布局进行分析
            param:
                house_data : 房屋数据
            return:
                pic : 绘制layout的分布
        '''
        f,ax1 = plt.subplots(figsize = (20,10))
        sns.countplot(y = 'Layout',data = self.house_data,ax = ax1)
        ax1.set_title('房屋户型布局')
        ax1.set_xlabel('数量')
        ax1.set_ylabel('户型')
        print(self.house_data['Renovation'].value_counts())
        plt.show()

    def renovation_analysis(self):
        '''
            对房子的装修情况进行特征分析
            param:
                house_data : 房屋信息
            return:
                pic : 绘制renovation的柱形图
        '''
        f,ax1 = plt.subplots(figsize = (10,7))
        sns.countplot(self.house_data['Renovation'],ax = ax1)
        plt.show()
        f,ax2 = plt.subplots(figsize = (10,7))
        sns.barplot(x = 'Renovation',y = 'Price',data = self.house_data,ax = ax2)
        plt.show()

    def elevator_analysis(self):
        '''
            对房子是否带电梯进行分析，并将数据
            param:
                house_data
            return:
                house_data
        '''
        #查看不带电梯的缺失值有多少户
        # print(house_data['Elevator'].value_counts())
        # print("电梯缺失值为:",len(house_data.loc[house_data['Elevator'].isnull()]))
        f,[ax1,ax2] = plt.subplots(1,2,figsize = (10,7))
        sns.countplot(self.house_data['Elevator'],ax = ax1)
        ax1.set_title('有无电梯对比')
        ax1.set_xlabel('是否有电梯')
        ax1.set_ylabel('数量')
        sns.barplot(x = 'Elevator',y = 'Price',data = self.house_data,ax = ax2)
        ax2.set_title('有无电梯房价对比')
        ax2.set_xlabel('是否有电梯')
        ax2.set_ylabel('总价')
        plt.show()

    def check_elevator(self):
        '''
            查看电梯属性，看看电梯在几楼之上普遍安装了电梯
            param:
                house_data
            return:
                None
        '''
        print(self.house_data['Elevator'].value_counts())
        print("电梯缺失值为:",len(self.house_data.loc[self.house_data['Elevator'].isnull()]))
        print(self.house_data['Floor'].describe())
        #从下面代码看的出超过6层后普遍有电梯
        for i in range(1,58):
            data = self.house_data.loc[self.house_data['Floor'] == float(i)]['Elevator']
            print("~~~~~~~~~~~~~~~~~~~~~~~~~",i)
            print(data.value_counts())

    def years_analysis(self):
        '''
            对year的分析
        '''
        grid = sns.FacetGrid(self.house_data,col="Renovation",row = "Elevator",height=5)
        grid.map(plt.scatter,"Year","Price",color = "g")
        # grid.add_legend()
        plt.show()

    def floor_analysis(self):
        '''
            对楼层进行特征分析
        '''
        f,ax1 = plt.subplots(figsize = (20,7))
        sns.countplot(x = "Floor",data = self.house_data,ax = ax1)
        ax1.set_title('房屋户型',fontsize=15)
        ax1.set_xlabel('楼层')
        ax1.set_ylabel('数量')
        plt.show()
